<?php


namespace app\queue\single;


use app\model\SysMessages;
use core\constants\QueueKey;
use core\utils\WsUtil;
use GatewayWorker\Lib\Gateway;
use support\Log;
use Throwable;
use Webman\RedisQueue\Consumer;

class SystemMessageNotify implements Consumer
{
    // 要消费的队列名
    public $queue = QueueKey::SYSTEM_MESSAGE_NOTIFY;

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费
    public function consume($data)
    {
        try {
            SysMessages::insert([
                "receiver_id" => $data['receiver_id'],
                "title" => $data['title'],
                "content" => $data['content'],
            ]);
            Gateway::sendToUid($data['receiver_id'], WsUtil::encode("system-message-notify", [
                'title' => $data['title'],
                'message' => $data['content'],
            ]));
        } catch (Throwable $exception) {
            dump($exception->getMessage());
            Log::error(sprintf("[%s] insert SystemMessageNotify [%s] queue-data: %s", SystemMessageNotify::class, $exception->getMessage(), json_encode($data, JSON_UNESCAPED_UNICODE)));
        }
    }
}